/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
// pprinter.hpp
//
// Base class for printer objects.
//
// Author: Jim DeLisle
//
// Copyright (c) 1997-2004 Swell Software
//              All Rights Reserved.
//
// Unauthorized redistribution of this source code, in whole or part,
// without the express written permission of Swell Software
// is strictly prohibited.
//
// Notes:
//
// This class defines all of the methods which must be supported in
// instantiated printer classes. Users may of course add additional members
// in custom derived classes, but only those members found here will be used
// by PEG.
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/

#ifndef _PPRINT_H_
#define _PPRINT_H_

#if defined(PEG_PRINTER_SUPPORT)

/*--------------------------------------------------------------------------*/
// error codes
/*--------------------------------------------------------------------------*/
enum
{
    PPRINT_ERR_ERROR  = -1, // general failure used in return values
    PPRINT_ERR_SUCCESS,     // successful operation
    PPRINT_ERR_SETUP,       // a part of mSetup is invalid
    PPRINT_ERR_UNSUPPORTED, // unsupported capability of the printer
    PPRINT_ERR_RES,         // selected resolution is unsupported
    PPRINT_ERR_COLOR_MODE,  // selected color mode is unsupported
    PPRINT_ERR_ORIENT,      // selected orientation is unsupported
    PPRINT_ERR_PAPER_SIZE,  // selected paper size is unsupported
    PPRINT_ERR_WIDTH,       // based on paper size and res, width is wrong
    PPRINT_ERR_HEIGHT,      // based on paper size and res, height is wrong
    PPRINT_ERR_COMM_INIT,   // unable to initialize communication
    PPRINT_ERR_COMM_DATA,   // data write failed
    PPRINT_ERR_ALLOC,       // failed to allocate the temp bitmap
    PPRINT_ERR_DRIVER,      // PegThing::mpPrint does not point to a printer
    PPRINT_ERR_NO_DOC,      // document has not been started
    PPRINT_ERR_NO_PAGE,     // no current page for the document
    PPRINT_ERR_CUR_DOC,     // already a document in progress
    PPRINT_ERR_CUR_PAGE,    // already a page in progess
    PPRINT_ERR_NO_PRINTER,  // the printer is not open
    PPRINT_ERR_NO_CONTEXT,  // could not make a draw context
    PPRINT_ERR_SUSPENDED,   // printing has been suspended
    PPRINT_ERR_NONE,        // no current error
    PPRINTER_ERR_MAX
};

/*--------------------------------------------------------------------------*/
// settings
//
// Note: Not all printers support all options
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
// Resoution
/*--------------------------------------------------------------------------*/
enum
{
    PPRINT_RES_75 = 75,
    PPRINT_RES_100 = 100,
    PPRINT_RES_150 = 150,
    PPRINT_RES_300 = 300,
    PPRINT_RES_600 = 600,
    PPRINT_RES_MAX
};

/*--------------------------------------------------------------------------*/
// Orientation
/*--------------------------------------------------------------------------*/
enum
{
    PPRINT_ORIENT_PORTRAIT,
    PPRINT_ORIENT_LANDSCAPE,
    PPRINT_ORIENT_MAX
};

/*--------------------------------------------------------------------------*/
// Color Mode
/*--------------------------------------------------------------------------*/
enum
{
    PPRINT_MODE_MONOCHROME,
    PPRINT_MODE_GRAYSCALE,
    PPRINT_MODE_COLOR_SIMPLE,
    PPRINT_MODE_COLOR,
    PPRINT_MODE_MAX
};

/*--------------------------------------------------------------------------*/
// Paper size
/*--------------------------------------------------------------------------*/
enum
{
    PPRINT_PS_LETTER,
    PPRINT_PS_LEGAL_LIST_1,
    PPRINT_PS_LEDGER,
    PPRINT_PS_EXECUTIVE,
    PPRINT_PS_A4,
    PPRINT_PS_A3,
    PPRINT_PS_COM_10,
    PPRINT_PS_MONARCH,
    PPRINT_PS_C5,
    PPRINT_PS_B5,
    PPRINT_PS_DL,
    PPRINT_PS_MAX
};

/*--------------------------------------------------------------------------*/
// defaults for width and height. These values in the Width and Height
// members of the setup struct inform the driver to use the default
// width and height of the given paper size for any resolution.
/*--------------------------------------------------------------------------*/
#define PPRINT_DEF_WIDTH    -1
#define PPRINT_DEF_HEIGHT   -1

/*--------------------------------------------------------------------------*/
// Settings structure
/*--------------------------------------------------------------------------*/
struct PegPrintSetup
{
    PEGINT ColorMode;   // of the valid color mode enum above
    PEGINT Orient;      // of the valid orientation enum above
    PEGINT Res;         // of the valid resolution enum above
    PEGINT PaperSize;   // of the valid paper size enum above
    PEGINT Width;       // expressed in pixels, not printer units
    PEGINT Height;      // expressed in pixels, not printer units

    PegPrintSetup(void)
    {
        ColorMode = PPRINT_MODE_MONOCHROME;
        Orient = PPRINT_ORIENT_PORTRAIT;
        Res = PPRINT_RES_75;
        PaperSize = PPRINT_PS_LETTER;
        Width = PPRINT_DEF_WIDTH;
        Height = PPRINT_DEF_HEIGHT;
    }

};

/*--------------------------------------------------------------------------*/
// Status structure
/*--------------------------------------------------------------------------*/
typedef struct
{
    PEGUINT DeviceOpen:1;
    PEGUINT BitmapAlloc:1;
    PEGUINT DocBegin:1;
    PEGUINT PageBegin:1;
    PEGUINT Suspended:1;
    PEGUINT Warning:1;
    PEGUINT Error:1;
} PegPrintStatus;

/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
class PegPrinter
{
    public:
        PegPrinter();
        virtual ~PegPrinter();

        virtual PEGINT BeginDoc(const PegPrintSetup *pSetup = NULL);
        virtual PEGINT BeginPage(void);
        virtual PEGINT EndDoc(void);
        virtual PEGINT EndPage(void);

        virtual PEGINT Cancel(void);
        virtual PEGINT Reset(void);
        virtual PEGINT Resume(void);
        virtual PEGINT Suspend(void);

        virtual PEGINT GetColorMode(void) const { return mSetup.ColorMode; }
        virtual PEGINT GetError(void) const { return mError; }
        virtual PEGINT GetOrientation(void) const { return mSetup.Orient;}
        virtual PEGINT GetPaperSize(void) const { return mSetup.PaperSize; }
        virtual PegRect GetPrintRect(void) const { return mPrintRect; }
        virtual PEGINT GetPrintSurface(void) const { return mPrintSurface; }
        virtual PEGINT GetResolution(void) const { return mSetup.Res; }
        virtual PEGBOOL IsOpen(void) const { return mStatus.DeviceOpen; }
        virtual PEGINT SetColorMode(PEGINT ColorMode);
        virtual PEGINT SetOrientation(PEGINT Orientation);
        virtual PEGINT SetPaperSize(PEGINT PaperSize);
        virtual PEGINT SetResolution(PEGINT Resolution);
        virtual PEGINT Setup(const PegPrintSetup *pSetup);

    protected:
        virtual PEGINT SurfaceToPrinter() = 0;
        virtual void ClearSurface(PEGCOLOR FillColor = CLR_TRANSPARENCY);
        virtual void CloseDevice();
        virtual PEGINT OpenDevice();
        virtual PEGINT SendData(const void* pData, PEGUINT dataLen);

        PegRect mPrintRect;
        PegPrintSetup mSetup;
        PegPrintStatus mStatus;
        PegThing *mpContext;
        PEGINT mError;
        PEGINT mPrintSurface;

    private:
       #ifdef PEGWIN32
        HANDLE mDevice;
       #elif defined(POSIXPEG) || defined(PEGX11)
        int mDevice;
       #endif
};

/*--------------------------------------------------------------------------*/
// Prototype for CreatePegPrint()-
//
// The actual implementation is in each individual derived PegPrinter class.
/*--------------------------------------------------------------------------*/
PegPrinter *CreatePegPrinter(void);

#endif  // PEG_PRINTER_SUPPORT
#endif  // _PPRINT_H_

